using UnityEngine;

namespace EntityKit.Runtime
{
    /// <summary>
    /// Vector2型可绑定属性，使用距离阈值比较两个Vector2是否相等
    /// </summary>
    public class Vector2Property : BindableProperty<Vector2>
    {
        private float mDistanceThreshold = 0.01f;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defaultValue">默认值</param>
        public Vector2Property(Vector2 defaultValue = default) : base(defaultValue)
        {
            WithComparer((a, b) => Vector2.Distance(a, b) < mDistanceThreshold);
        }

        /// <summary>
        /// 设置距离比较阈值
        /// </summary>
        /// <param name="threshold">新的距离阈值</param>
        /// <returns>当前实例</returns>
        public Vector2Property WithDistanceThreshold(float threshold)
        {
            mDistanceThreshold = Mathf.Max(0, Mathf.Abs(threshold));
            WithComparer((a, b) => Vector2.Distance(a, b) < mDistanceThreshold);
            return this;
        }
    }
}